/**
 * 问卷系统 API Service
 */
import request from '@/http';
import {
  QuestionBank, Question, QuestionListItem, Paper, PaperListItem,
  Submission, SubmissionListItem, QuestionComment,
  QuestionStatistics, PaperStatistics,
  CreatePaperRequest, CreateQuestionRequest, SubmitAnswer,
  QuestionType, DifficultyLevel, PaperStatus
} from '@/types/question';
import { PaginatedResponse } from '@/types/common';
import { AxiosResponse } from 'axios';
import { ResponseData } from '@/http/types';

const Url = {
  questionBanks: '/api/question-banks/',
  questions: '/api/questions/',
  papers: '/api/papers/',
  submissions: '/api/submissions/',
};

export const questionService = {
  // ==================== 题库管理 ====================
  
  // 获取题库列表
  async getQuestionBanks(params?: {
    keyword?: string;
    page?: number;
    limit?: number;
  }): Promise<AxiosResponse<ResponseData<PaginatedResponse<QuestionBank>>>> {
    return request.get(Url.questionBanks, { params });
  },

  // 获取题库详情
  async getQuestionBank(id: number): Promise<AxiosResponse<ResponseData<QuestionBank>>> {
    return request.get(`${Url.questionBanks}${id}/`);
  },

  // 创建题库
  async createQuestionBank(data: Partial<QuestionBank>): Promise<AxiosResponse<ResponseData<QuestionBank>>> {
    return request.post(Url.questionBanks, data);
  },

  // 更新题库
  async updateQuestionBank(id: number, data: Partial<QuestionBank>): Promise<AxiosResponse<ResponseData<QuestionBank>>> {
    return request.patch(`${Url.questionBanks}${id}/`, data);
  },

  // 删除题库
  async deleteQuestionBank(id: number): Promise<AxiosResponse<ResponseData<null>>> {
    return request.delete(`${Url.questionBanks}${id}/`);
  },

  // 获取题库中的题目
  async getQuestionBankQuestions(
    bankId: number,
    params?: {
      type?: QuestionType;
      difficulty?: DifficultyLevel;
      page?: number;
      limit?: number;
    }
  ): Promise<AxiosResponse<ResponseData<PaginatedResponse<QuestionListItem>>>> {
    return request.get(`${Url.questionBanks}${bankId}/questions/`, { params });
  },

  // ==================== 题目管理 ====================

  // 获取题目列表
  async getQuestions(params?: {
    bank_id?: number;
    type?: QuestionType;
    difficulty?: DifficultyLevel;
    tags?: string;
    keyword?: string;
    page?: number;
    limit?: number;
  }): Promise<AxiosResponse<ResponseData<PaginatedResponse<QuestionListItem>>>> {
    return request.get(Url.questions, { params });
  },

  // 获取题目详情
  async getQuestion(id: number): Promise<AxiosResponse<ResponseData<Question>>> {
    return request.get(`${Url.questions}${id}/`);
  },

  // 创建题目
  async createQuestion(data: CreateQuestionRequest): Promise<AxiosResponse<ResponseData<Question>>> {
    return request.post(Url.questions, data);
  },

  // 更新题目
  async updateQuestion(id: number, data: Partial<CreateQuestionRequest>): Promise<AxiosResponse<ResponseData<Question>>> {
    return request.patch(`${Url.questions}${id}/`, data);
  },

  // 删除题目
  async deleteQuestion(id: number): Promise<AxiosResponse<ResponseData<null>>> {
    return request.delete(`${Url.questions}${id}/`);
  },

  // 获取题目评论
  async getQuestionComments(
    questionId: number,
    params?: { page?: number; limit?: number }
  ): Promise<AxiosResponse<ResponseData<PaginatedResponse<QuestionComment>>>> {
    return request.get(`${Url.questions}${questionId}/comments/`, { params });
  },

  // 发表评论
  async createComment(
    questionId: number,
    data: { content: string; parent?: number }
  ): Promise<AxiosResponse<ResponseData<QuestionComment>>> {
    return request.post(`${Url.questions}${questionId}/comments/`, data);
  },

  // 获取题目统计
  async getQuestionStatistics(): Promise<AxiosResponse<ResponseData<QuestionStatistics>>> {
    return request.get(`${Url.questions}statistics/`);
  },

  // ==================== 试卷管理 ====================

  // 获取试卷列表
  async getPapers(params?: {
    status?: PaperStatus;
    keyword?: string;
    page?: number;
    limit?: number;
  }): Promise<AxiosResponse<ResponseData<PaginatedResponse<PaperListItem>>>> {
    return request.get(Url.papers, { params });
  },

  // 获取试卷详情
  async getPaper(id: number): Promise<AxiosResponse<ResponseData<Paper>>> {
    return request.get(`${Url.papers}${id}/`);
  },

  // 创建试卷
  async createPaper(data: CreatePaperRequest): Promise<AxiosResponse<ResponseData<Paper>>> {
    return request.post(Url.papers, data);
  },

  // 更新试卷
  async updatePaper(id: number, data: Partial<CreatePaperRequest>): Promise<AxiosResponse<ResponseData<Paper>>> {
    return request.patch(`${Url.papers}${id}/`, data);
  },

  // 删除试卷
  async deletePaper(id: number): Promise<AxiosResponse<ResponseData<null>>> {
    return request.delete(`${Url.papers}${id}/`);
  },

  // 开始答题
  async startPaper(id: number): Promise<AxiosResponse<ResponseData<Submission>>> {
    return request.post(`${Url.papers}${id}/start/`);
  },

  // 保存答案（暂存）
  async saveAnswer(
    paperId: number,
    data: {
      question_id: number;
      answer: any;
      time_spent?: number;
    }
  ): Promise<AxiosResponse<ResponseData<null>>> {
    return request.post(`${Url.papers}${paperId}/save_answer/`, data);
  },

  // 提交试卷
  async submitPaper(
    paperId: number,
    data: { answers: SubmitAnswer[] }
  ): Promise<AxiosResponse<ResponseData<Submission>>> {
    return request.post(`${Url.papers}${paperId}/submit/`, data);
  },

  // 获取试卷的所有提交记录（管理员）
  async getPaperSubmissions(
    paperId: number,
    params?: { page?: number; limit?: number }
  ): Promise<AxiosResponse<ResponseData<PaginatedResponse<SubmissionListItem>>>> {
    return request.get(`${Url.papers}${paperId}/submissions/`, { params });
  },

  // 获取试卷统计
  async getPaperStatistics(paperId: number): Promise<AxiosResponse<ResponseData<PaperStatistics>>> {
    return request.get(`${Url.papers}${paperId}/statistics/`);
  },

  // ==================== 提交记录管理 ====================

  // 获取提交记录列表
  async getSubmissions(params?: {
    paper_id?: number;
    user_id?: number;
    page?: number;
    limit?: number;
  }): Promise<AxiosResponse<ResponseData<PaginatedResponse<SubmissionListItem>>>> {
    return request.get(Url.submissions, { params });
  },

  // 获取提交详情
  async getSubmission(id: number): Promise<AxiosResponse<ResponseData<Submission>>> {
    return request.get(`${Url.submissions}${id}/`);
  },

  // 获取我的提交记录
  async getMySubmissions(params?: {
    page?: number;
    limit?: number;
  }): Promise<AxiosResponse<ResponseData<PaginatedResponse<SubmissionListItem>>>> {
    return request.get(`${Url.submissions}my_submissions/`, { params });
  },
};

export default questionService;
